package com.cn.ant.modules.school.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.common.utils.Identities;
import com.cn.ant.modules.school.dao.MajorInfoDao;
import com.cn.ant.modules.school.entity.MajorInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专业信息Service
 * @author AntDream
 * @version 2015-08-19
 */
@Service
@Transactional(readOnly = true)
public class MajorInfoService extends BaseService {

    @Autowired
	private MajorInfoDao majorInfoDao;

	public MajorInfo get(String id) {
		return majorInfoDao.get(id);
	}

    public MajorInfo getByCode(String majorCode) {
        return majorInfoDao.getByCode(majorCode);
    }

	public List<MajorInfo> findList(MajorInfo majorInfo) {
		return majorInfoDao.findList(majorInfo);
	}
	
	public Page<MajorInfo> findPage(Page<MajorInfo> page, MajorInfo majorInfo) {
	    majorInfo.setPage(page);
        page.setList(majorInfoDao.findList(majorInfo));
        return page;
	}

    public List<Map<String,String>> queryMajorList(String majorLevel) {
        Map<String, String> params = new HashMap<>();
        params.put("majorLevel", majorLevel);
        return majorInfoDao.queryMajorList(params);
    }

    public List<Map<String,String>> queryMajorParentCode(String majorLevel){
        Map<String, String> params = new HashMap<>();
        params.put("majorLevel", majorLevel);
        return majorInfoDao.queryMajorParentCode(params);
    }

    public List<Map<String,String>> queryFavMajor(String userId) {
        Map<String, String> params = new HashMap<>();
        params.put("userId", userId);
        return majorInfoDao.queryFavMajor(params);
    }

    @Transactional(readOnly = false)
	public void save(MajorInfo majorInfo) {
	    if (majorInfo.getIsNewRecord()) {
            majorInfo.preInsert();
            majorInfoDao.insert(majorInfo);
        } else {
            majorInfo.preUpdate();
            majorInfoDao.update(majorInfo);
        }
	}

    @Transactional(readOnly = false)
	public int batchInsert(List<MajorInfo> majorInfoList) {
        if (majorInfoList != null && majorInfoList.size() > 0) {
            for (int i = 0; i < majorInfoList.size(); i++) {
                majorInfoList.get(i).preInsert();
                majorInfoList.get(i).setId(Identities.generateUUID());
            }
            return majorInfoDao.batchInsert(majorInfoList);
        }
        return 0;
    }

    @Transactional(readOnly = false)
	public void delete(MajorInfo majorInfo) {
		majorInfoDao.delete(majorInfo);
	}
	
}